/**========================== */
// 将毫秒转化为  XX:XX:XX 格式
const formatNumber = n => {
  return n>9 ? n : '0' + n
}

const formatTimes = date =>{
  var hours = Math.floor(date / 3600);
  var minute = Math.floor((date - 3600*hours) / 60);
  var seconds = date - hours * 60 * 60 - minute * 60;
  return formatNumber(hours) + ":" + formatNumber(minute) + ":" + formatNumber(seconds);
}


/**========================== */

/**生成随机数*/
const randomStr = (len)=>{
  len = len || 32;
  //var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  var $chars = '123456789';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  var maxPos = $chars.length;
  var pwd = '';
  for (var i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}


/**========================== */
// 登录生成图片验证码

const verification = (str)=>{
  return "https://api.cloudcpc.com/home/Login/create?id=" + str;
}


/**========================== */
//登录注册验证手机号
const  validateTel = (tel) => {
  var TEL_REGEXP = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/;
  if (TEL_REGEXP.test(tel)) {
    return true;
  }
  return false;
}


// 将日期 秒值转换为 xxxx年xx月xx日
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "h+": this.getHours(), //小时 
    "m+": this.getMinutes(), //分 
    "s+": this.getSeconds(), //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds() //毫秒 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

//val传入的是毫秒值，将此值转化为 xxxx年xx月xx日
function dateformat(val) {

  var time1 = new Date(val).Format("yyyy-MM-dd");

  var arr = time1.split('-'),
      strarr = ['年', '月', '日'],
      str = '';
  arr.forEach(function(v,i){
    str+=v+strarr[i]
  });

  return str;
}




module.exports = {
  formatTimes: formatTimes,
  randomStr: randomStr,
  verification: verification,
  validateTel: validateTel,
  dateformat: dateformat
}